#include "players.h"

/*Player-------------------------------
 * Its a class that represents a player.
 * It stores all of its fleets,
 * resourcces, etc and handles updates at
 *  the end of the turn.
 ------------------------------------*/
//constructors---------------
Players::player::player(int id, string name) {
    playerId = id;
    playerName = name;
    endedTurn = false;
}
Players::player::~player() {
}
//access---------------------
string  Players::player::gName(){
    return playerName;
}
Players::fleetmanager* Players::player::gFleetManager() {
    endedTurn = true;
    return &Fleets;
}
bool Players::player::played() {
    return endedTurn;
}
//manips---------------------
void Players::player::reset() {
    endedTurn = false;
}
void Players::player::endTurn() {
    Fleets.endTurn();
    Players::playerLib::gStaticPlayerLib()->endTurn();
}
/*playerLib--------------------------------------
 * the player library. It keeps track of the current
 * turn, whose next, and updating bases at the last
 * turn.
 ----------------------------------------------*/
Players::playerLib* Players::playerLib::staticPlayerLib = NULL;
//constructors----------------
Players::playerLib::playerLib() {
    currentPlayer = 0;
}
Players::playerLib::~playerLib() {
}
Players::playerLib* Players::playerLib::gStaticPlayerLib() {
    if (staticPlayerLib == NULL) {
        staticPlayerLib = new playerLib();
    }
    return staticPlayerLib;
}
void Players::playerLib::newPlayer(string str){
    player p(players.size(), str);
    players.push_back(p);
}
//access----------------------
Players::player* Players::playerLib::gPlayer(int id){
    if(id >= 0 && id < players.size()) return &players[id];
    return NULL;
}
Players::player* Players::playerLib::gCurentPlayer() {
    return &players[currentPlayer];
}
int Players::playerLib::gCurrentPlayerId() {
    return currentPlayer;
}
//manips---------------------
void Players::playerLib::setTurnOrderPolicy(turnOrder o) {
    orderPolicy = o;
}
void Players::playerLib::endTurn() {
    players[currentPlayer].endTurn();
    int start = currentPlayer;
    if (orderPolicy == random) start = rand();
    while (players[currentPlayer].played()) {
        start = currentPlayer++;
        if (currentPlayer >= players.size()) currentPlayer = 0;
        if (start == currentPlayer) resetAll();
    }
}
void Players::playerLib::resetAll() {
    for (int i = 0; i < players.size(); i++) {
        players[i].reset();
    }
}